package com.shidaimanong.demo.encypt;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 学编程，关注：十代码农
 * 抖音、快手、B站、西瓜视频、今日头条
 */
public class Md5Demo {

    public static void main(String[] args) {
        System.out.println(toMd5("123456"));
    }

    public static String toMd5(String plainText) {
        byte[] secretBytes;
        try {
            secretBytes = MessageDigest.getInstance("md5").digest(plainText.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("不支持md5算法！");
        }
        //字节码转16进制
        String md5code = new BigInteger(1, secretBytes).toString(16);
        //如果不足32位，前面补0
        for (int i = 0; i < 32 - md5code.length(); i++) {
            md5code = "0" + md5code;
        }
        return md5code;
    }
}